<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>ec: include/ec/timer.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">ec
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_086fd96841d95816c032b124cf477f3d.html">ec</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">timer.h</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * timer.h</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> *  Created on: 2015年5月31日</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> *      Author: havesnag</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;</div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#ifndef EC_TIMER_H_</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor"></span><span class="preprocessor">#define EC_TIMER_H_</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="preprocessor"></span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#include &quot;loop.h&quot;</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;</div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="keyword">namespace </span>ec</div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;{</div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;</div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="keyword">class </span>Loop;</div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div>
<div class="line"><a name="l00022"></a><span class="lineno"><a class="line" href="classec_1_1Timer.html">   22</a></span>&#160;<span class="keyword">class </span><a class="code" href="classec_1_1Timer.html">Timer</a></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;{</div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">   26</a></span>&#160;    <span class="keyword">typedef</span> std::function&lt;void ()&gt; <a class="code" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">Handler</a>;</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;    <a class="code" href="classec_1_1Timer.html">Timer</a>(<span class="keyword">const</span> <a class="code" href="classec_1_1Loop.html">ec::Loop</a> &amp;loop);</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;    <span class="keyword">virtual</span> ~<a class="code" href="classec_1_1Timer.html">Timer</a>();</div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Timer.html#ad48f591873f207f843a3fc8b306063bf">startRounds</a>(uint32_t <a class="code" href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">interval</a>, uint64_t <a class="code" href="classec_1_1Timer.html#ae576336f9adac852fcf1172e0c740960">round</a>, <a class="code" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">ec::Timer::Handler</a> handler);</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Timer.html#af066807719d25dd78919346b1bc28d17">startOnce</a>(uint32_t <a class="code" href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">interval</a>, <a class="code" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">ec::Timer::Handler</a> handler);</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Timer.html#ac103f227ad5f5bd44a29c24e37aabc20">startForever</a>(uint32_t <a class="code" href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">interval</a>, <a class="code" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">ec::Timer::Handler</a> handler);</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Timer.html#a3bbcca634bad30417438a085abe31d25">startAfter</a>(</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            uint32_t after,</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;            uint32_t <a class="code" href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">interval</a>,</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;            uint64_t <a class="code" href="classec_1_1Timer.html#ae576336f9adac852fcf1172e0c740960">round</a>,</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;            <a class="code" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">ec::Timer::Handler</a> handler);</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;</div>
<div class="line"><a name="l00068"></a><span class="lineno"><a class="line" href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">   68</a></span>&#160;    <span class="keyword">inline</span> uint32_t <a class="code" href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">interval</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;        <span class="keywordflow">return</span> _interval;</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;    }</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;</div>
<div class="line"><a name="l00074"></a><span class="lineno"><a class="line" href="classec_1_1Timer.html#ae576336f9adac852fcf1172e0c740960">   74</a></span>&#160;    <span class="keyword">inline</span> uint64_t <a class="code" href="classec_1_1Timer.html#ae576336f9adac852fcf1172e0c740960">round</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;        <span class="keywordflow">return</span> _round;</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;    }</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;</div>
<div class="line"><a name="l00080"></a><span class="lineno"><a class="line" href="classec_1_1Timer.html#a447daf396b3eade6cdea14912575525a">   80</a></span>&#160;    <span class="keyword">inline</span> uint64_t <a class="code" href="classec_1_1Timer.html#a447daf396b3eade6cdea14912575525a">curRound</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;        <span class="keywordflow">return</span> _curRound;</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;    }</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;</div>
<div class="line"><a name="l00086"></a><span class="lineno"><a class="line" href="classec_1_1Timer.html#a97095d71c497ae7e47b01bffb9383542">   86</a></span>&#160;    <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Timer.html#a97095d71c497ae7e47b01bffb9383542">isFinished</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;        <span class="keywordflow">return</span> _curRound &gt;= _round;</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;    }</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;    <span class="keywordtype">void</span> reset();</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;    <span class="keyword">static</span> <span class="keywordtype">void</span> eventHandler(evutil_socket_t fd, <span class="keywordtype">short</span> events, <span class="keywordtype">void</span> *ctx);</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;    <span class="keyword">const</span> <a class="code" href="classec_1_1Loop.html">Loop</a> &amp;_loop;</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;    <span class="keyword">struct </span>event *_event;</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;    uint32_t _interval; <span class="comment">//定时时长，以毫秒为单位</span></div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;    uint64_t _round; <span class="comment">//总周期</span></div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;    uint64_t _curRound; <span class="comment">//当前周期</span></div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;    <a class="code" href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">ec::Timer::Handler</a> _handler;</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;};</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;} <span class="comment">/* namespace ec */</span></div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;<span class="preprocessor">#endif </span><span class="comment">/* EC_TIMER_H_ */</span><span class="preprocessor"></span></div>
<div class="ttc" id="classec_1_1Timer_html_af632191a18c3b476fe052404643046c6"><div class="ttname"><a href="classec_1_1Timer.html#af632191a18c3b476fe052404643046c6">ec::Timer::interval</a></div><div class="ttdeci">uint32_t interval() const </div><div class="ttdef"><b>Definition:</b> timer.h:68</div></div>
<div class="ttc" id="classec_1_1Timer_html_a97095d71c497ae7e47b01bffb9383542"><div class="ttname"><a href="classec_1_1Timer.html#a97095d71c497ae7e47b01bffb9383542">ec::Timer::isFinished</a></div><div class="ttdeci">bool isFinished() const </div><div class="ttdef"><b>Definition:</b> timer.h:86</div></div>
<div class="ttc" id="classec_1_1Timer_html"><div class="ttname"><a href="classec_1_1Timer.html">ec::Timer</a></div><div class="ttdoc">定时器类。 </div><div class="ttdef"><b>Definition:</b> timer.h:22</div></div>
<div class="ttc" id="classec_1_1Timer_html_af066807719d25dd78919346b1bc28d17"><div class="ttname"><a href="classec_1_1Timer.html#af066807719d25dd78919346b1bc28d17">ec::Timer::startOnce</a></div><div class="ttdeci">bool startOnce(uint32_t interval, ec::Timer::Handler handler)</div><div class="ttdoc">启动单次定时器 </div><div class="ttdef"><b>Definition:</b> timer.cpp:64</div></div>
<div class="ttc" id="classec_1_1Timer_html_ae576336f9adac852fcf1172e0c740960"><div class="ttname"><a href="classec_1_1Timer.html#ae576336f9adac852fcf1172e0c740960">ec::Timer::round</a></div><div class="ttdeci">uint64_t round() const </div><div class="ttdef"><b>Definition:</b> timer.h:74</div></div>
<div class="ttc" id="classec_1_1Timer_html_a447daf396b3eade6cdea14912575525a"><div class="ttname"><a href="classec_1_1Timer.html#a447daf396b3eade6cdea14912575525a">ec::Timer::curRound</a></div><div class="ttdeci">uint64_t curRound() const </div><div class="ttdef"><b>Definition:</b> timer.h:80</div></div>
<div class="ttc" id="classec_1_1Timer_html_acbf2889a6472ca60ce4d3c8856b717c5"><div class="ttname"><a href="classec_1_1Timer.html#acbf2889a6472ca60ce4d3c8856b717c5">ec::Timer::Handler</a></div><div class="ttdeci">std::function&lt; void()&gt; Handler</div><div class="ttdoc">定时器回调函数 </div><div class="ttdef"><b>Definition:</b> timer.h:26</div></div>
<div class="ttc" id="classec_1_1Timer_html_a3bbcca634bad30417438a085abe31d25"><div class="ttname"><a href="classec_1_1Timer.html#a3bbcca634bad30417438a085abe31d25">ec::Timer::startAfter</a></div><div class="ttdeci">bool startAfter(uint32_t after, uint32_t interval, uint64_t round, ec::Timer::Handler handler)</div><div class="ttdoc">在指定事件后启动定时器 </div><div class="ttdef"><b>Definition:</b> timer.cpp:74</div></div>
<div class="ttc" id="classec_1_1Loop_html"><div class="ttname"><a href="classec_1_1Loop.html">ec::Loop</a></div><div class="ttdoc">事件循环，对event_base的封装。 </div><div class="ttdef"><b>Definition:</b> loop.h:23</div></div>
<div class="ttc" id="classec_1_1Timer_html_ad48f591873f207f843a3fc8b306063bf"><div class="ttname"><a href="classec_1_1Timer.html#ad48f591873f207f843a3fc8b306063bf">ec::Timer::startRounds</a></div><div class="ttdeci">bool startRounds(uint32_t interval, uint64_t round, ec::Timer::Handler handler)</div><div class="ttdoc">启动多轮循环定时器 </div><div class="ttdef"><b>Definition:</b> timer.cpp:29</div></div>
<div class="ttc" id="classec_1_1Timer_html_ac103f227ad5f5bd44a29c24e37aabc20"><div class="ttname"><a href="classec_1_1Timer.html#ac103f227ad5f5bd44a29c24e37aabc20">ec::Timer::startForever</a></div><div class="ttdeci">bool startForever(uint32_t interval, ec::Timer::Handler handler)</div><div class="ttdoc">启动无限循环定时器 </div><div class="ttdef"><b>Definition:</b> timer.cpp:69</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Tue Jul 12 2016 14:15:08 for ec by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>
